package com.shiro.demo.Realm;

import com.shiro.demo.Dao.UserDao;
import com.shiro.demo.Dao.UserdaoImol;
import com.shiro.demo.vo.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.HashService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.util.*;

public class CustomRealm extends AuthorizingRealm {
    @Resource
      private UserdaoImol userDao;
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String userName= (String) principalCollection.getPrimaryPrincipal();
        Set<String> roles=GetRolesByuserName(userName);
        Set<String> permissions=getPermissionsByuserName(userName);
        SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
        authorizationInfo.setStringPermissions(permissions);
        authorizationInfo.setRoles(roles);
        return authorizationInfo;
    }

    private Set<String> getPermissionsByuserName(String userName) {
        List<String> perms = userDao.queryPermsByName(userName);
        Set<String> set = new HashSet<String>(perms);
        return set;
    }

    private Set<String> GetRolesByuserName(String userName) {
        System.out.println("从asaaaa数据库里取数据");
        List<String> list=userDao.queryRolesBYuserName(userName);
        Set<String> sets=new HashSet<>(list);

        return sets;
    }

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username= (String) authenticationToken.getPrincipal();
        System.out.println(username);
        String password=getPasswordByuesrname(username);
        System.out.println(password);
        if(password==null){
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo=new SimpleAuthenticationInfo(username,password,"customRealm");
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(username));
        return  authenticationInfo;
    }

    /**
     * 模拟数据库查询凭证
     * @param username
     * @return
     */
    private String getPasswordByuesrname(String username) {
        System.out.println("从数据库取数据");
       User user =userDao.getPasswordByuserName(username);

        if (user!=null){

            return user.getPassword();
        }
        return null;
    }

    public static void main(String[] args) {
        Md5Hash md5Hash=new Md5Hash("123456","Mark");
        System.out.println(md5Hash.toString());
    }
}
